package com.tanhua.interceptor;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.domain.User;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Administrator
 */
public class TokenInterceptor1 extends HandlerInterceptorAdapter {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求
        String token = request.getHeader("Authorization");
        boolean b = JwtUtils.verifyToken(token);
        //如果token 失效
        if(b==false){
            response.setStatus(401);
            return false;
        }
        //如果token正确,放行
        //解析token 获取id和电话号码
        Claims claims = JwtUtils.getClaims(token);
        String mobile = (String) claims.get("mobile");
        Long id = (Long) claims.get("id");
        //创建user对象 存入threadLocad
        User user = new User();
        user.setId(id);
        user.setMobile(mobile);
        UserHolder1.set(user);

        return true;

    }
}